<template>
	<view style="background-color: #F6FAF;">
		<view class="pl25 pr25" v-if="isUserId">
			<view class="pl20 pr20 bb box flex flex-ac" style="margin-bottom: 30rpx;margin-top: 50rpx;">
				<view class="it1 flex flex-ac">
					<text style="font-size: 30rpx;">找不到收货地址信息？绑定手机号试试</text>
					<button class='login_btn' style="line-height: 1.5;padding-left: 1;background-color: #F73809;font-size: 30rpx;margin-right: 1rpx;" type='primary' open-type="getPhoneNumber"  @getphonenumber="getPhoneNumber">
					  登录
					</button>
				</view>
			</view>
		</view>
		<view class="pl25 pr25" v-if="medDocInfoId!=0" style="padding-bottom: 140rpx;">
			<radio-group @change="radioChange">
				<view v-for="(item,index) in addressList" :key="item.id">
					<view class="pt30 pb30 bb">
						<view class="flex flex-ac">
							<text class="ft16 bold">{{item.name}}</text>
							<text class="ml20 type1 c74a6fd ft12" style="color: #2A82E4;">{{item.phone}}</text>
							<text class="ml20 c74a6fd ft12" v-if="item.isdefault ==1" style="color: #2A82E4;">默认</text>
						</view>
						<view class="flex mt15">
							<view class="it1">
								<view>{{item.address}}</view>
							</view>
							<image class="edit" src="http://tuozhen1.oss-cn-beijing.aliyuncs.com/ueditor/upload/weixin/images/edit.png" @click="editAddress(item.id)"></image>
						</view>
						<radio :value="item.id" :checked="item.isdefault ==1" style="transform:scale(0.8);margin-top: 10rpx;" color="#F28C87"/>
					</view>
				</view>
			</radio-group>
		</view>
		
		<view v-if="medDocInfoId==0" style="padding-bottom: 140rpx;" class="pl25 pr25">
				<view v-for="(item,index) in addressList" :key="item.id" @click="editAddress(item.id)" 
					style="position: relative;margin-top: 20rpx;background-color: #ffffff !important;padding: 0rpx 20rpx;">
					<view class="pt30 pb30">
						<view class="flex flex-ac">
							<text class="ft16 bold">{{item.name}}</text>
							<text class="ml20 type1 c74a6fd ft12" style="color: #2A82E4;">{{item.phone}}</text>
							<text class="ml20 c74a6fd ft12" v-if="item.isdefault ==1"  style="color: #2A82E4;">默认</text>
						</view>
						<view class="flex mt15">
							<view class="it1">
								<view>{{item.address}}</view>
							</view>
							<!-- <view style="color: #F73809;margin-right: 10rpx;">删除</view> -->
							<image class="edit" src="http://tuozhen1.oss-cn-beijing.aliyuncs.com/ueditor/upload/weixin/images/edit.png"></image>
						</view>
					</view>
					<image src="../../static/images/xx.png"
					style="height: 42rpx;width: 42rpx;border-radius: 50%;
						position: absolute;top:-10rpx;right: -10rpx;" @click.stop="delAddress(item.id)"></image>	
				</view>
		</view>
		
		<!-- <view class="bottomBtn ftc" v-if="medDocInfoId!=0">
			<view class="bottomBtncon bgfff">
				<text class="cfff btns ct ftc block" @click="returnAdvisoryvideo">完成</text>
			</view>
		</view>
		<view class="bottomBtn ftc"  v-if="!isUserId">
			<view class="bottomBtncon bgfff">
				<text class="cfff btns ct ftc block" @click="addAddress">＋ 新增收货地址</text>
			</view>
		</view> -->
		
		<view style="width: 100%;position: fixed;bottom: 40rpx;display: flex;align-items: center;">
			<view :class="[medDocInfoId!=0?'add-btn-2':'add-btn']" @click="addAddress" v-if="!isUserId">+新增收货地址</view>
			<view class="add-btn-2" @click="returnAdvisoryvideo" v-if="medDocInfoId!=0">完成</view>
		</view>
		
		<uni-popup ref="popup" type="message">
		    <uni-popup-message :type="warnType" :message=warnMessage :duration="warnTime"></uni-popup-message>
		</uni-popup>
	</view>
</template>

<script>
	
	const WxAuth =  require('@/utils/wxlogin');
	export default {
		data() {
			return {
				warnMessage:'错误',//消息内容
				warnType:'warn',//消息类型  success/warn/info/error
				warnTime:1000,//消息提醒延长时间
				isUserId:false,
				isNum:0,
				addressList: [], //收货地址列表
				adressInfo:null, //收货地址
				medDocInfoId: 0,
				userId: null,
				videofee: null,
				consultant: null,
				personalFileId: 0,
				medcalRecordId: null,
				aimkey: null,
				recipe:'',//判断是否是处方详情跳转过来的
				addressId:'',//收货地址id
				addressUserName:'',//收货人
				defaultaddress:'',//默认地址
			};
		},
		onLoad(option) {
			if(typeof option.recipe!='undefined'){
				this.recipe = option.recipe;
			}
			console.log(this.recipe)
			this.isNum=option.isNum;
			if(option.medDocInfoId=='undefined'||option.medDocInfoId==undefined){
				this.medDocInfoId=0
			}else{
				this.medDocInfoId = option.medDocInfoId;
			}
			this.userId = option.userId;
			this.videofee = option.videofee;
			this.consultant = option.consultant;
			this.personalFileId = option.personalFileId;
			this.medcalRecordId = option.medcalRecords;
			this.aimkey = option.aimkey;
		},
		onShow() {
			var usrUserId = uni.getStorageSync("usrUserId");
			if(usrUserId){
				this.isUserId = false;
				this.userId=usrUserId;
				//初始化数据
				this.initData(this.userId);
			}else{
				this.isUserId = true;
			}
		},
		methods: {
			//登录
			getPhoneNumber(e){
				var that = this;
				if(e.detail.errMsg.indexOf('fail')==-1){
					that.encryptedData = e.detail.encryptedData;
					that.iv = e.detail.iv;
					that.getCode(e.detail.encryptedData,e.detail.iv,this.hspId);
				}
			},
			//获取用户id并存入缓存
			getCode(encryptedData,iv,hspId){
				var that = this;
				WxAuth.checkSession().then(code => {
				  WxAuth.getUserIdByPhone(code,encryptedData,iv,hspId).then(res =>{
						  if(res.success){
							that.warnMessage = '登录成功';
							that.warnType = 'success';
							that.$refs.popup.open();
							that.loginHX(res.result);//登录环信
							that.isUserId = false;
							that.userId = res.result;
							//初始化数据关注医生数量
							that.initData(res);
						  }else{
							that.warnMessage = res.errMsg;
							that.$refs.popup.open();
						  }
					  })
				  })
			},
			//登录环信
			// loginHX(userId){
			// 	var test_account = userId+'';
			// 	var test_psword = uni.getStorageSync("test_psword")
			// 	uni.setStorageSync("usrUserId",test_account)
			// 	uni.setStorageSync("myUsername",test_account);
			// 	this.$conn.open({
			// 		apiUrl: this.$im.config.apiURL,
			// 		user: test_account,
			// 		pwd: test_psword,
			// 		grant_type: "password",
			// 		appKey: this.$im.config.appkey
			// 	});
			// },
			initData(userId) {
				var usrUserId = userId; //用户id
				this.func.$request('applyRecipe/queryAddressList.tz',{usrUserId: usrUserId},'GET').then(res=>{
					if (res.data.success) {
						this.addressList = res.data.result;
						for(var i=0;i<this.addressList.length;i++){
							if(this.addressList[i].isdefault==1){
								this.defaultaddress=this.addressList[i].address
								this.addressId=this.addressList[i].id
								this.addressUserName==this.addressList[i].name
							}
						}
					}
				})
				
			},
			//新增收货地址
			addAddress() {
				uni.navigateTo({
					url: '/pages/addressadd/index?userId='+this.userId
				});
			},
			//修改收货地址
			editAddress(obj) {
				uni.navigateTo({
					url: '/pages/addressadd/index?userId='+this.userId+'&addressId=' + obj
				});
			},
			delAddress(objId){
				this.func.$request('wx/deleteAddress.tz',{id: objId},'GET').then(res=>{
					if (res.data.success) {
						uni.showToast({
							title:'删除成功',
							icon:'success'
						})
						this.initData(this.userId);
					}
				})
			},
			//选择收货地址
			radioChange(evt) {
				this.addressId = evt.target.value;
				for (let i = 0; i < this.addressList.length; i++) {
					if (this.addressList[i].id == evt.target.value) {
						this.adressInfo = this.addressList[i].address;
						this.addressUserName = this.addressList[i].name;
						break;
					}
				}
			},
			//返回视频咨询或者图文咨询页面
			returnAdvisoryvideo() {
				if(this.adressInfo==null){
					this.adressInfo=this.defaultaddress
				}
				if(this.recipe!=''){
					var address = {};
					address.addressId = this.addressId;
					address.adressInfo = this.adressInfo;
					address.addressUserName = this.addressUserName;
					uni.navigateBack({//返回
					     delta: 1
				   })
				   //定义一个监听事件
				   uni.$emit('returnPres', address);
				}else{
					uni.setStorageSync("adressInfo", this.adressInfo)
					let pages = getCurrentPages();
					let prevPage = pages[pages.length-2]
					prevPage.$vm.adressInfo = this.adressInfo
					uni.navigateBack({
						delta:1
					})
				}
			}
		},
	};
</script>

<style lang="less">
	.add-btn{
		margin: auto;
		height: 88rpx;
		line-height: 88rpx;
		width: 90%;
		background: #BE965B;
		border-radius: 44rpx;
		color: #FFFFFF;
		text-align: center;
	}
	.add-btn-2{
		margin: auto;
		height: 88rpx;
		line-height: 88rpx;
		width: 45%;
		background: #BE965B;
		border-radius: 44rpx;
		color: #FFFFFF;
		text-align: center;
	}
	.edit {
		width: 32rpx;
		height: 32rpx;
	}

	.type1 {
		background: #d7e5fe;
		line-height: 32rpx;
		padding: 0 15rpx;
	}

	.bottomBtn {
		margin-top: 30rpx;
		position: relative;
		height: 125rpx;
		width: 100%;

		.bottomBtncon {
			position: absolute;
			width: 700rpx;
			z-index: 10;
			left: 25rpx;
			height: 125rpx;
			bottom: 0;
			background: #FFFFFF;

			.btns {
				width: 660rpx;
				height: 100rpx;
				line-height: 100rpx;
				border-radius: 10rpx;
				background: #74a6fd;
			}
		}
	}
</style>
